import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhangyilin on 2018/11/26.
 *
 * 显示学生自己加入的班级
 */


@WebServlet("/ShowStuClasses")
public class ShowStuClasses extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ShowStuClasses() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setContentType("text/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        response.setCharacterEncoding("UTF-8");//setContentType与setCharacterEncoding的顺序不能调换，否则还是无法解决中文乱码的问



        Connection conn=null;

        String str=" Show Student Of All Classes ";
        String title = new String(str.getBytes("ISO8859-1"),"UTF-8");
        String docType = "<!DOCTYPE html>\n";
        out.println(docType +
                "<html>\n" +
                "<head>" +
                "<title>" + title + "</title>" +
                "</head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
//                "<h1 align=\"center\">" + title + "</h1>" +
                "\n");

//        String stuid=request.getParameter("stuID");
        String stuid = (String) request.getSession().getAttribute("ID");
//        out.print("<h3 align=\"center\">StudentID of the class is "+stuid+"</h3>");
        out.println(" <br/><center>");
        try {

            // 打开一个连接
            conn = GetConnection();

             //在SClass表中查看学生加入的班级信息
                String sql2="select InviteID from SClass where StudentID=? ";
                PreparedStatement pst2 = null;
                pst2 = conn.prepareStatement(sql2);
                pst2.setString(1, stuid);
                ResultSet  rs2 = pst2.executeQuery();//查询学生
                out.println("<table><tr><td><h4>"+"ClassName"+"</h4></td>"+"<td><h4>Teacher</h4></td>"+
                        "<td><h4>InviteID</h4></td>"+
                        "</tr>");//Teacher对应班级创建者，为显示方便
                String[] inviteID=null;

                while(rs2.next()) {
                    //获得该班级的所有学生学号的字符串，利用split进行拆分成学生学号数组
                    inviteID=rs2.getString("InviteID").split(",");

                    //将数组里的各个学号分别查询User_t表中的姓名，并显示出来
                    for (int i = 0; i < inviteID.length; i++) {
                        String class_inviteID = inviteID[i];
                        String sql3="select ClassName,ClassBlonger,InviteID from Class_t where InviteID=?";

                        PreparedStatement pst3 = null;
                        pst3 = conn.prepareStatement(sql3);
                        pst3.setString(1, class_inviteID);
                        ResultSet  rs3 = pst3.executeQuery();

                        while(rs3.next()) {
                            out.println("<tr><td>"+rs3.getString("ClassName")+"</td>");
                            out.println("<td>"+rs3.getString("ClassBlonger")+"</td>");
                            out.println("<td>"+rs3.getString("InviteID")+"</td></tr>");

                        }
                        rs3.close();
                        pst3.close();
                    }

                }

                out.println("</table>");
                rs2.close();
                pst2.close();

            out.println("</center>");
            out.println("</body></html>");
            conn.close();
        } catch(SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 最后是用于关闭资源的块
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }




    }


    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }


}

